package cn.fancylab.template.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.fancylab.model.bs.BaseGoods;
import cn.fancylab.model.bs.DeviceCabinetBox;
import cn.fancylab.model.bs.DeviceController;
import cn.fancylab.model.bs.DeviceGateway;
import cn.fancylab.service.DaoService;
import cn.fancylab.template.util.JsonUtil;
import cn.fancylab.web.support.FancyVelocityView;
import cn.fancylab.web.support.JsonStrDirectOut;

@Controller
public class AjaxController extends AnnotationController {

	@Autowired
	@Qualifier("daoService")
	private DaoService daoService;

	@RequestMapping(value = "/ajax/deviceController.xhtml")
	public String deviceController() {
		DetachedCriteria query = DetachedCriteria.forClass(DeviceController.class);
		query.add(Restrictions.eq("is_del", 0));
		List list = daoService.findByCriteria(query);

		HttpServletRequest request = getRequest();
		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(list)));
		return "common/directOut.vm";
	}

	@RequestMapping(value = "/ajax/devicecabinetBox.xhtml")
	public String deviceCabinetBox() {
		DetachedCriteria query = DetachedCriteria.forClass(DeviceCabinetBox.class);
		query.add(Restrictions.eq("is_del", 0));
		List list = daoService.findByCriteria(query);

		HttpServletRequest request = getRequest();
		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(list)));
		return "common/directOut.vm";
	}

	@RequestMapping(value = "/ajax/deviceGateway.xhtml")
	public String deviceGateway() {
		DetachedCriteria query = DetachedCriteria.forClass(DeviceGateway.class);
		query.add(Restrictions.eq("is_del", 0));
		List list = daoService.findByCriteria(query);

		HttpServletRequest request = getRequest();
		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(list)));
		return "common/directOut.vm";
	}

	@RequestMapping(value = "/ajax/basegoods.xhtml")
	public String baseGoods() {
		DetachedCriteria query = DetachedCriteria.forClass(BaseGoods.class);
		query.add(Restrictions.eq("is_del", 0));
		List list = daoService.findByCriteria(query);

		HttpServletRequest request = getRequest();
		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(list)));
		return "common/directOut.vm";
	}
}